package com.yd.mq.parse.task;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.yd.mq.parse.cache.AlarmActiveCacheUtils;
import com.yd.mq.parse.model.AlarmRecord;
import com.yd.mq.parse.model.AlarmRecordTask;
import com.yd.mq.parse.model.ObservedAlarm;
import com.yd.mq.parse.service.AlarmRecordService;
import com.yd.mq.parse.service.ObservedAlarmService;

/**
 * 定时读取文件
 * 
 * @ClassName AutoReadFileTasks
 * @author Zuolg
 * @date 2017年8月24日 上午10:42:50
 */
@Service
public class AlarmActiveTasks {
	/* 日志处理 */
	private Logger					log				= Logger.getLogger(this.getClass().getName());
	private ExecutorService			executorService	= Executors.newCachedThreadPool();

	/* 数据入库服务 */
	@Resource
	private AlarmRecordService		alarmRecordService;

	/* 数据入库服务 */
	@Resource
	private ObservedAlarmService	observedAlarmService;

	@Scheduled(cron = "${alarm.activeTask.cron}") // 配置时间
	public void fixTimeExecution() {
		if (AlarmActiveCacheUtils.getInstance().list.size() > 0) {
			log.info("执行多个激活告警定时任务!");
		}
		executorService.execute(new Runnable() {
			@Override
			public void run() {
				AlarmActiveOperation();
			}
		});

	}

	/**
	 * 多个激活告警操作 @return void @throws
	 */
	@Async
	private void AlarmActiveOperation() {
		// 当前时间
		Calendar calendar = Calendar.getInstance();

		// 删除列表
		List<AlarmRecordTask> removeList = new ArrayList<>();

		Iterator<AlarmRecordTask> iterator = AlarmActiveCacheUtils.getInstance().list.iterator();
		while (iterator.hasNext()) {
			AlarmRecordTask alarmRecordTask = iterator.next();
			if (calendar.getTime().getTime() > alarmRecordTask.getOverdueTime().getTime()) {
				// // 当前时间>过期时间，说明已经过期，修改当前记录
				AlarmRecord alarmRecord = alarmRecordTask.getAlarmRecord();
				if (alarmRecord != null) {
					alarmRecord.setCancelReceivedTime(alarmRecordTask.getCancelTime());
					alarmRecord.setAlarmCancelTime(alarmRecordTask.getCancelTime());
					// 此处设置为激活的系列号
					alarmRecord.setAlarmCancelSerial(alarmRecord.getAlarmActiveSerial());
					alarmRecordService.update(alarmRecord);
					log.info("任务修改记录AlarmRecord_id:" + alarmRecord.getId());
					ObservedAlarm observedAlarm = observedAlarmService.findBy("recordId", alarmRecord.getId());
					if (observedAlarm != null && alarmRecordTask.getCancelTime() != null) {
						observedAlarm.setCancelTime(alarmRecordTask.getCancelTime());
						observedAlarmService.update(observedAlarm);
						log.info("任务修改记录ObservedAlarm_id:" + observedAlarm.getId());
					}
				}

				removeList.add(alarmRecordTask);

			}
		}

		// 移除操作后的内容
		for (AlarmRecordTask alarmRecordTaskRemove : removeList) {
			AlarmActiveCacheUtils.getInstance().list.remove(alarmRecordTaskRemove);
		}

	}

}
